package com.example.administrator.sixangleview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.NonNull;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ImageView;

import com.example.administrator.sixangleview.MainActivity.ViewBean;
import com.example.administrator.sixangleview.R;
import com.example.administrator.sixangleview.UtilTools;

public class SixangleImageViews extends ImageView {
    private int mLength;
    private Paint paint;

    private int color;
    private float textSize;

    private String texts;
    private Bitmap backBitmap;
    private float degrees;
    private boolean isRotate;
    private double RADIAN;

    private int[] colors = {getResources().getColor(R.color.color_flight),
            getResources().getColor(R.color.color_train),
            getResources().getColor(R.color.color_setting),
            getResources().getColor(R.color.color_sales),
            getResources().getColor(R.color.color_hotel),
            getResources().getColor(R.color.color_user),
            getResources().getColor(R.color.color_remind)};

    private Bitmap[] bitmaps = {
            BitmapFactory.decodeResource(getResources(), R.mipmap.icon_flight),
            BitmapFactory.decodeResource(getResources(), R.mipmap.train),
            BitmapFactory.decodeResource(getResources(), R.mipmap.icon_hotel),
            BitmapFactory.decodeResource(getResources(), R.mipmap.icon_user),
            BitmapFactory.decodeResource(getResources(), R.mipmap.icon_cx),
            BitmapFactory.decodeResource(getResources(), R.mipmap.icon_hbtx),
            BitmapFactory.decodeResource(getResources(), R.mipmap.icon_setting)

    };

    public SixangleImageViews(Context context) {
        super(context);
    }

    public SixangleImageViews(Context context, ViewBean bean) {
        super(context);
        setCustomAttributes(bean);
        init();
    }

    private void setCustomAttributes(ViewBean bean) {
        textSize = bean.getTextSize();
        texts = bean.getTexts();
        backBitmap = bitmaps[bean.getCode()];
        color = colors[bean.getColor()];
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(color);
        paint.setAlpha(150);
        degrees = 0;
        isRotate = false;
        RADIAN = Math.PI / 180;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(SixangleViewGroup.LENGTH, SixangleViewGroup.LENGTH);
    }

    /**
     * 不要把Path、Matrix作为全局变量，否则性能开销很大，会有卡顿
     * Matrix设为全局变量还会导致逻辑错误，正中心六边形的图片会绘制错误
     * 因为Matrix每次都是右乘矩阵，所以每次都要New，否则会在上一次的矩阵基础上再右乘新矩阵
     *
     * @param canvas
     */
    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        mLength = getWidth() / 2;
        int centerX = mLength;
        int centerY = getHeight() / 2;

        if (isRotate) {
            canvas.rotate(degrees, centerX, centerY);
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(color);
            paint.setAlpha(150);
        }

        //画六边形
        Path path = new Path();
        path.moveTo(getWidth(), centerY);
        path.lineTo(centerX + endCoordinateX(60), centerY + endCoordinateY(60));
        path.lineTo(centerX + endCoordinateX(120), centerY + endCoordinateY(120));
        path.lineTo(centerX + endCoordinateX(180), centerY + endCoordinateY(180));
        path.lineTo(centerX + endCoordinateX(240), centerY + endCoordinateY(240));
        path.lineTo(centerX + endCoordinateX(300), centerY + endCoordinateY(300));
        path.close();
        canvas.drawPath(path, paint);

        paint.reset();
        paint.setAntiAlias(true);

        // 画背景
        Matrix matrix = new Matrix();
        matrix.postTranslate(centerX - backBitmap.getWidth() / 2,
                centerY - backBitmap.getHeight() / 2);
        canvas.drawBitmap(backBitmap, matrix, paint);

        // 写文字
        paint.setColor(Color.WHITE);
        paint.setTextSize(textSize);
        canvas.drawText(texts, centerX - 20, getHeight() - 24, paint);
    }

    private float endCoordinateX(float degree) {
        return (float) (mLength * Math.cos(degree * RADIAN));
    }

    private float endCoordinateY(float degree) {
        return (float) (mLength * Math.sin(degree * RADIAN));
    }

    public void addDegrees(double degree) {
        degrees += degree;
        if (degrees >= 360)
            degrees = 0;
    }

    public void setRotate(boolean isRotate) {
        this.isRotate = isRotate;
    }

}
